package com.ys.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.ys.mapper.CgoderMapper;
import com.ys.mapper.DrguoqiMapper;
import com.ys.mapper.DruMapper;
import com.ys.mapper.DrugsMapper;
import com.ys.pojo.Cgoder;
import com.ys.pojo.Drguoqi;
import com.ys.pojo.Dru;
import com.ys.pojo.Drugs;
import com.ys.service.AdministratorsService;
import com.ys.vo.PageVo;
import com.ys.vo.ResultVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

@Controller
public class CaigouContorller {
    @Resource
    private DruMapper druMapper;
    @RequestMapping(value = "/xunigouwu", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo add(@RequestBody Drugs drugs){
        ResultVo resultVo = new ResultVo();
        Dru dru = new Dru();
        dru.setDrugsid(drugs.getDrugsid());
        dru.setDrugsName(drugs.getDrugsName());
        dru.setNumberNumber(drugs.getNumber());
        dru.setDanjia(drugs.getPrice());
        dru.setSpecifications(drugs.getSpecifications());
        druMapper.add(dru);
        return resultVo;
    }


    @RequestMapping("/selectxunigouwu")
    @ResponseBody
    public String select(){
//        mvn clean package /Dmaven.text.s
        List<Dru> list = druMapper.select();
        list.forEach(x->{
            x.setShus(x.getNumberNumber());
        });
        return JSON.toJSONString(list);
    }


    @RequestMapping("/deldelxunigouwu")
    @ResponseBody
    public ResultVo deldelxunigouwu(){
        ResultVo resultVo = new ResultVo();
        resultVo.setMsg(Integer.toString(druMapper.del()));
        return resultVo;
    }

    @RequestMapping("/deldelxunigouwuididid")
    @ResponseBody
    public ResultVo deldelxunigouwuididid(@RequestBody Dru dru){
        ResultVo resultVo = new ResultVo();
        resultVo.setMsg(Integer.toString(druMapper.dels(dru.getId())));
        return resultVo;
    }
    @RequestMapping("/piliangshanchu")
    @ResponseBody
    public ResultVo piliangshanchu(Integer[] ids){
        ResultVo resultVo = new ResultVo();
        for (int i = 0; i < ids.length; i++) {
            Integer.toString(druMapper.dels(ids[i]));
        }
        resultVo.setMsg("0");
        return resultVo;
    }

    @RequestMapping("/updasesss")
    @ResponseBody
    public ResultVo updasesss(@RequestBody Dru dru){
        ResultVo resultVo = new ResultVo();
        System.out.println(dru);
        resultVo.setMsg(Integer.toString(druMapper.updas(Double.valueOf(dru.getShu()),dru.getId())));
        return resultVo;
    }

    @Resource
    private CgoderMapper cgoderMapper;
    @Resource
    private AdministratorsService administratorsService;

    @RequestMapping("/chaxunselectcaigoudinganssssssssssss")
    @ResponseBody
    public ResultVo daochudaochu(int pageNo,int pageSize,String spuid,String ids,String id){
        ResultVo resultVo = new ResultVo();
        PageHelper.startPage(pageNo,pageSize);
        List<Cgoder> list = cgoderMapper.selectCgoderssssssss(spuid,ids,id);
        list.forEach(x->{
                if(x.getQualPer()!=null){
                    x.setZhijianren(administratorsService.finalByAllobj(x.getQualPer()).getAnames());
                }
        });
        resultVo.setObj(new PageVo(list));
        return resultVo;
    }


    /**
     * 打印导出
     * @param fmt
     * @param gender
     * @return
     */
    @RequestMapping("/export")
    public ModelAndView export(String fmt, Integer gender){
        ModelAndView mav=new ModelAndView();
        mav.setViewName("aa");
        mav.addObject("format",fmt);
        mav.addObject("id",gender);
        return mav;
    }

    @Resource
    private DrugsMapper drugsMapper;
    @Resource
    private DrguoqiMapper drguoqiMapper;

    /**
     * 定时器，没10分钟进行一次数据库查询
     * @return
     */
    @RequestMapping("/jishiqi")
    @ResponseBody
    public ResultVo jishiqi(){
        ResultVo resultVo = new ResultVo();
        List<Drugs> list  = drugsMapper.dingshiqi();
        for (Drugs x:list){
//            每次查询都要检查当前编号
            if(drguoqiMapper.count(String.valueOf(x.getDrugsid()))==0){
                Drguoqi drguoqi = new Drguoqi();
                drguoqi.setDrugsid(x.getDrugsid());//编号
                drguoqi.setDrugsName(x.getDrugsName());//名称
                drguoqi.setNumberNumber(x.getNumber());//数量
                drguoqi.setPrice(x.getPrice());//单价
                drguoqi.setYaopinguoqi(x.getBaozhiqi());//过期时间
                drguoqi.setYaopinshengchan(x.getManufacture());//生产时间
                drguoqiMapper.addinsertssss(drguoqi);
            }
        }

        if(drguoqiMapper.guoqi()!=0){
            resultVo.setMsg("有过期商品，请进行处理。");
        }
        return resultVo;
    }



}
